import CONFIG from './config.js'

var host
if(CONFIG.ENV == 'TEST'){ host = CONFIG.TEST_HOST } 
if(CONFIG.ENV == 'PROD'){ host = CONFIG.PROD_HOST }

function service(options = {}){
	let token = uni.getStorageSync('token');
  let tenantId = uni.getStorageSync('tenantId')?uni.getStorageSync('tenantId'):'1';//登录时默认传1，登录后拿到了tenantId值再覆盖掉
  let merId = uni.getStorageSync('merId')?uni.getStorageSync('merId'):'1';//登录时默认传1，登录后拿到了merId值再覆盖掉
  
	console.log('tenantId'+tenantId);
	//console.log(1111199);
	let array1 = {'Authori-zation':token,'tenant-id':tenantId,'mer-id':merId};
  console.log('获取缓存的对象')
  // console.log(JSON.parse(uni.getStorageSync('adminInfoCache')))
	//options = array;
	//console.log(options.header);
  options.header= array1
	// if(token){ options.header= array1}
	//console.log(options.header);
	options.url = host+options.url
	return new Promise((resolved,reject)=>{
		options.success = (res) => {
			//console.log(res);
			if(res&&Number(res.data.code)==401){
				uni.showToast({title:'您还未登录，请先登录',icon:'none'})
				setTimeout(()=>{
					uni.navigateTo({url:'/pages/login/login'})
				},1500)
				return
			}
			if(res&&Number(res.data.code)==500){
				//console.log('9988',res);
				uni.showToast({title:res.data.message,icon:'none'})
				return
			}
			if(res) resolved(res.data);
		}
		options.fail = (err) =>{ reject(err) }
		uni.request(options)
	}).catch((e)=>{})
}

export default service